import { Controller, Get, Post, Req, Res } from '@nestjs/common';
import { Response } from 'express';
import AiChat from 'src/_common/lib/OpenAi';

@Controller('chat')
export class ChatController {
    @Post('sendMessage')
     async sendMessage(@Req() req: Request, @Res() res: Response){
        
        let msg = req.body as any;

        let aiChat = new AiChat();

        let streamRes = await aiChat.doChat(msg.content);
        res.writeHead(200, {
            // 'Content-Type': 'application/json; charset=utf-8',
            // 'Transfer-Encoding': 'chunked'
        })
        for await (const chunk of streamRes) {
          let msg = chunk.choices[0]?.delta?.content || "";
          res.write(JSON.stringify({ id:'systemMsgId', content:msg })+"\n");
        }
        res.end();
    }
}
